﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ClassLibraryMessageDataType;
using ClassLibraryMessageDispacher;
using ClassLibraryMessageDataType;
namespace GUI
{
    /// <summary>
    ///  UI发给DB的所有方法
    /// </summary>
   public   class ClassUI2DBCMDCollections
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="mesSend"></param>
        /// <param name="mesRecv"></param>
        /// <returns></returns>
        static private bool sendAndrecvMessage2DBbase(ref MessagePackage mesSend, out MessagePackage mesRecv)
        {
           return GUICommunicationClass.instance.Sent2DBbasevMessage(ref mesSend, out mesRecv);         
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="destIP"></param>
        /// <param name="strucobj"></param>
        /// <param name="squence"></param>
        /// <param name="mesRecv"></param>
        /// <returns></returns>
        private static bool Creat2SentMessage(object strucobj, ushort squence, ProductType senType,bool isNeedreback, out MessagePackage mesOut)
        {
            mesOut = null;
            byte[] getbytes = StructByteConver.StructToBytes(strucobj);       
            ArraySegment<byte> buf1 = new ArraySegment<byte>(getbytes);
            SoureDetailInfo soinfo = new SoureDetailInfo();
            soinfo.sourepath = MessagePackagePath.UICom;
            DestDetailInfo deinfo = new DestDetailInfo();
            deinfo.destpath = MessagePackagePath.DBCom;
            deinfo.DB = "";
            mesOut = new MessagePackage(
                senType,
                0,
                squence,
                isNeedreback,
                MessagePackagePath.UICom,
                MessagePackagePath.DBCom,
                soinfo,
                deinfo,
                "",
                buf1);
            return true;

        }


        public static bool NotifyStartWorkSteps( string ip,string time)
        {
            try
            {
                //===========1变化============用户自己构造对象

                DBMessageDataType_NotifyStart2WkorkSteps st = new DBMessageDataType_NotifyStart2WkorkSteps();

                //===========1变化============用户自己构造对象

                //============2固定填写==============          
                ushort squence = GUICommunicationClass.instance.GetSquence();
                //===========2固定填写===============

                //===========3变化============用户自己填入字段
                st.IP = new byte[4];
                st.time = new byte[14];
                st.cmd = ClassDBcmdDefine.START_WORKSTEPS;
                string[] ipSplit = ip.Split('.');
                st.IP[0] = byte.Parse(ipSplit[0]);
                st.IP[1] = byte.Parse(ipSplit[1]);
                st.IP[2] = byte.Parse(ipSplit[2]);
                st.IP[3] = byte.Parse(ipSplit[3]);

                 st.time = System.Text.Encoding.ASCII.GetBytes(time);
            
                
                //===========3变化==================

                //===========4固定填写==============
                if (false == Creat2SentMessage(st, squence,ProductType.notifyevent, false,out MessagePackage sentMes))
                {
                    throw new Exception("Creat2SentMessage check sum err!");
                }
                bool res = sendAndrecvMessage2DBbase(ref sentMes, out MessagePackage outMes);
                //===========4固定填写==============
                if (res == true)
                {
                    // string strcmds = string.Format("main cmd {2x} sub cmd{2x:1}", st.mainCMD, st.subCMD);
                  if(outMes != null) Console.WriteLine(" db 223 SUCCESS :  get current data Cmd sq :" + outMes.squence.ToString() + " \r\n");
                }
                else
                {
                    Console.WriteLine("db cmd:" + "time out\r\n");
                }
                return res;


            }
            catch (Exception e)
            {
                throw e;
            }

        }


        public static bool NotifyWorkStepsChangeEvent_changeUnityInfosWatchBoard(string ip, string time ,byte status,byte worksteptype)
        {
            try
            {
                //===========1变化============用户自己构造对象

                DBMessageDataType_NotifyChangeWkorkSteps_updateWatchBoard st = new DBMessageDataType_NotifyChangeWkorkSteps_updateWatchBoard();

                //===========1变化============用户自己构造对象

                //============2固定填写==============          
                ushort squence = GUICommunicationClass.instance.GetSquence();
                //===========2固定填写===============

                //===========3变化============用户自己填入字段
                st.IP = new byte[4];
                st.time = new byte[14];
                st.cmd = ClassDBcmdDefine.START_WORKSTEPCHANGE_EVENT_CHANGENUTIFYWATCHBAORD;
                string[] ipSplit = ip.Split('.');
                st.IP[0] = byte.Parse(ipSplit[0]);
                st.IP[1] = byte.Parse(ipSplit[1]);
                st.IP[2] = byte.Parse(ipSplit[2]);
                st.IP[3] = byte.Parse(ipSplit[3]);

                st.time = System.Text.Encoding.ASCII.GetBytes(time);

                st.status = status;
                st.workstepType = worksteptype;
                //===========3变化==================

                //===========4固定填写==============
                if (false == Creat2SentMessage(st, squence, ProductType.notifyevent, false, out MessagePackage sentMes))
                {
                    throw new Exception("Creat2SentMessage check sum err!");
                }
                bool res = sendAndrecvMessage2DBbase(ref sentMes, out MessagePackage outMes);
                //===========4固定填写==============
                if (res == true)
                {
                    // string strcmds = string.Format("main cmd {2x} sub cmd{2x:1}", st.mainCMD, st.subCMD);
                    if (outMes != null) Console.WriteLine(" db 223 SUCCESS :  get current data Cmd sq :" + outMes.squence.ToString() + " \r\n");
                }
                else
                {
                    Console.WriteLine("db cmd:" + "time out\r\n");
                }
                return res;


            }
            catch (Exception e)
            {
                throw e;
            }

        }

        public static bool NotifyWorkStepsChangeEvent_updateall640data(string ip, string time, byte status, byte worksteptype,ref OneClientAll64BoardData all640)
        {
            try
            {
                //===========1变化============用户自己构造对象

                DBMessageDataType_NotifyChangeWkorkSteps_updateAll640data st = new DBMessageDataType_NotifyChangeWkorkSteps_updateAll640data();

                //===========1变化============用户自己构造对象

                //============2固定填写==============          
                ushort squence = GUICommunicationClass.instance.GetSquence();
                //===========2固定填写===============

                //===========3变化============用户自己填入字段
                st.IP = new byte[4];
                st.time = new byte[14];
                st.cmd = ClassDBcmdDefine.START_WORKSTEPCHANGE_EVENT_INSERTALL640POINTDATA;
                string[] ipSplit = ip.Split('.');
                st.IP[0] = byte.Parse(ipSplit[0]);
                st.IP[1] = byte.Parse(ipSplit[1]);
                st.IP[2] = byte.Parse(ipSplit[2]);
                st.IP[3] = byte.Parse(ipSplit[3]);

                st.time = System.Text.Encoding.ASCII.GetBytes(time);

                st.status = status;
                st.workstepType = worksteptype;
                st.all640data = all640;
                //===========3变化==================

                //===========4固定填写==============
                if (false == Creat2SentMessage(st, squence, ProductType.notifyevent, false, out MessagePackage sentMes))
                {
                    throw new Exception("Creat2SentMessage check sum err!");
                }
                bool res = sendAndrecvMessage2DBbase(ref sentMes, out MessagePackage outMes);
                //===========4固定填写==============
                if (res == true)
                {
                    // string strcmds = string.Format("main cmd {2x} sub cmd{2x:1}", st.mainCMD, st.subCMD);
                    if (outMes != null) Console.WriteLine(" db 223 SUCCESS :  get current data Cmd sq :" + outMes.squence.ToString() + " \r\n");
                }
                else
                {
                    Console.WriteLine("db cmd:" + "time out\r\n");
                }
                return res;


            }
            catch (Exception e)
            {
                throw e;
            }

        }

        public static bool RquestUsersInfo(out string name,out string pw)
        {
            name = string.Empty;
            pw = string.Empty;
            try
            {
                //===========1变化============用户自己构造对象

                DBMessageDataType_RquestUserInfo st = new DBMessageDataType_RquestUserInfo();

                //===========1变化============用户自己构造对象

                //============2固定填写==============          
                ushort squence = GUICommunicationClass.instance.GetSquence(); 
                //===========2固定填写===============

                //===========3变化============用户自己填入字段
                st.cmd = 0X00;           
                //===========3变化=================

                //===========4固定填写==============
                if (false == Creat2SentMessage(st, squence, ProductType.request, true, out MessagePackage sentMes))
                {
                    throw new Exception("Creat2SentMessage check sum err!");
                }
                bool res = sendAndrecvMessage2DBbase(ref sentMes, out MessagePackage outMes);
                //===========4固定填写==============
                if (res == true)
                {
                    // string strcmds = string.Format("main cmd {2x} sub cmd{2x:1}", st.mainCMD, st.subCMD);
                    Console.WriteLine("SUCCESS :  get current data Cmd sq :" + outMes.squence.ToString() + " \r\n");
                }
                else
                {
                    Console.WriteLine("db cmd:" +  "time out\r\n");
                }
                return res;


            }
            catch (Exception e)
            {
                throw e;
            }

        }

    }

}
